//
//  SearchTableViewCell.m
//  CapriceMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "DownLoadManager.h"
@implementation SearchTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.myImageView.layer.cornerRadius = 12;
        self.myImageView.clipsToBounds = YES;
        [self addSubview:self.myImageView];
        
        self.mvName = [[UILabel alloc] initWithFrame:CGRectZero];
        self.mvName.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.mvName];
        
        self.singerName = [[UILabel alloc] initWithFrame:CGRectZero];
        self.singerName.font = [UIFont systemFontOfSize:12];
        self.singerName.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
        [self addSubview:self.singerName];
        
        self.playLaber = [[UILabel alloc] initWithFrame:CGRectZero];
        self.playLaber.font = [UIFont systemFontOfSize:12];
        self.playLaber.textColor = [UIColor grayColor];
        [self addSubview:self.playLaber];
        
        self.playNumber = [[UILabel alloc] initWithFrame:CGRectZero];
        self.playNumber.font = [UIFont systemFontOfSize:12];
        self.playNumber.textColor = [UIColor grayColor];
        [self addSubview:self.playNumber];
        
        self.functionButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.functionButton setImage:[[UIImage imageNamed:@"point"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.functionButton addTarget:self action:@selector(functionDidPress:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.functionButton];
        
        
    }
    return self;
}


- (void)setSearchMvModel:(searchMVModel *)searchMvModel {
    if (_searchMvModel != searchMvModel) {
        _searchMvModel = searchMvModel;
    }
    
    [self.myImageView sd_setImageWithURL:[NSURL URLWithString:searchMvModel.albumImg]];
    
    self.mvName.text = searchMvModel.title;
    
    self.singerName.text = searchMvModel.artistName;
    
    self.playLaber.text = @"播放次数:";
    
    self.playNumber.text = [NSString stringWithFormat:@"%@", searchMvModel.totalViews.stringValue];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myImageView.frame = CGRectMake(0, 0, self.width / 2.2, self.height - 2);
    self.mvName.frame = CGRectMake(self.myImageView.right + 5, 15, self.width - self.myImageView.width - 20, 10);
    self.singerName.frame = CGRectMake(self.mvName.origin.x, self.mvName.bottom + 10, self.mvName.width - 20, 10);
    self.playLaber.frame = CGRectMake(self.singerName.origin.x, self.singerName.bottom + 10, 60, 10);
    self.playNumber.frame = CGRectMake(self.playLaber.right, self.playLaber.origin.y, self.width - self.myImageView.width - self.playLaber.width, 10);

    self.functionButton.frame = CGRectMake(self.width - 20, self.height / 2, 15, 15);

    self.functionButton.frame = CGRectMake(self.width - 30, self.height / 2.3, 25, 20);

    
}

- (void)functionDidPress:(UIButton *)sender {
    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"下载", @"收藏", nil];
    actionsheet.actionSheetStyle = UIActionSheetStyleAutomatic;
    [actionsheet showInView:self];
}

#warning download
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"%@",self.searchMvModel.title);
       DownLoadManager *down = [DownLoadManager shareDownLoadManager];
        if (down.isdownloading == YES) {
            UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在当前任务结束后重试" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [view show];
        } else {
            [down downLoad:self.searchMvModel.url];
            DataBaseHandle *data = [DataBaseHandle shareDataBase];
            [data openDataBase];
            [data createDownloadingTable];
            [data insertDownloadingDatabase:self.searchMvModel.title withPicture:self.searchMvModel.thumbnailPic withDownloadUrl:self.searchMvModel.url];
            [data closedDataBase];
        
        }
            }else if (buttonIndex == 1) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        BOOL mybool = YES;
        DataBaseHandle *dataBaseHandle = [DataBaseHandle shareDataBase];
        [dataBaseHandle openDataBase];
        [dataBaseHandle createTable];
        NSMutableArray *array = [dataBaseHandle selectDataBase];
        if (array.count == 0) {
            [dataBaseHandle insertDatabase:self.searchMvModel.title withids:self.searchMvModel.ids.stringValue withType:@"VIDEO" withImageUrl:self.searchMvModel.albumImg];
    
            [center postNotificationName:@"collect" object:@"收藏成功" userInfo:nil];
        }else {
            for (NSDictionary *dic in array) {
                if ([[dic valueForKey:@"ids"] isEqualToString:self.searchMvModel.ids.stringValue]) {
                    [center postNotificationName:@"collect" object:@"亲,您已经收藏过了" userInfo:nil];
                    mybool = NO;
                    break;
                }
            }
            if (mybool) {
                [dataBaseHandle insertDatabase:self.searchMvModel.title withids:self.searchMvModel.ids.stringValue withType:@"VIDEO" withImageUrl:self.searchMvModel.albumImg];
                [center postNotificationName:@"collect" object:@"收藏成功" userInfo:nil];
            }
            [dataBaseHandle closedDataBase];
        }
    }
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {
    NSLog(@"actionSheetCancel");
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
//    NSLog(@"didDismiss");
}

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
//    NSLog(@"willDismiss");
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
